package com.yanqu.road.server.cmd.yrank.union;

import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.rank.yrank.UnionYRank;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.StrongholdWarProto;
import com.yanqu.road.pb.rank.YRankProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.biaoju.CrossBiaoJuActivity;
import com.yanqu.road.server.manager.activity.biaoju.CrossBiaoJuMgr;
import com.yanqu.road.server.manager.yrank.CrossYRankMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.List;

@Cmd(code = CrossProtocol.C2_CROSS_YRANK_GET_UNION_RANK_NUM, desc = "")
public class CrossYRankGetUnionRankNumCmd extends CrossUnionCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        int rankNum = -1;
        String value = "0";
        String param = "";

        YRankProto.YRankReqMsg reqMsg = YRankProto.YRankReqMsg.parseFrom(packet.getBytes());

        int conditionType = Integer.valueOf(reqMsg.getIdKey());
        if (conditionType == eGamePlayerEventType.BiaoJuUnionExpRank.getValue()) {
            CrossBiaoJuActivity activity = CrossBiaoJuMgr.getActivity(reqMsg.getActivityId(), reqMsg.getGroupId());
            if (activity != null) {
                activity.sendUnionRank(packet.getPlayerId(), serverId);
            }
            return;
        }

        UnionYRank unionYRank = CrossYRankMgr.getUnionYRank(reqMsg.getActivityId(), reqMsg.getGroupId(), reqMsg.getIdKey(), packet.getPlayerId());
        if (unionYRank != null) {
            value = unionYRank.getValue().toString();
            param = unionYRank.getParam();

            List<UnionYRank> rankList = CrossYRankMgr.getSortUnionRankList(reqMsg.getActivityId(), reqMsg.getGroupId(), reqMsg.getIdKey());
            rankNum = CrossYRankMgr.getUnionRankNum(rankList, packet.getPlayerId());
        }

        StrongholdWarProto.StrongholdActivityUnionRankListRespMsg.Builder msg = StrongholdWarProto.StrongholdActivityUnionRankListRespMsg.newBuilder();
        msg.setMyRank(rankNum);
        msg.setValue(value);
        msg.setParam(param);
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(ClientProtocol.U_CROSS_GENERAL_ACTIVITY_UNION_RANK, msg);
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), pbMsg);
    }

    @Override
    public void sendData() {

    }
}
